package com.tensquare.web;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class WebZuulFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        System.out.println("web前台网关，经过了zuul前置过滤器。。。");

        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        String authorization = request.getHeader("Authorization");
        if(null != authorization){
            //可以判断请求uri
            String requestURI = request.getRequestURI();
            //打印网关转发的uri: /qa/problem
            //System.out.println("------"+requestURI);
            //如果有，则转发头信息
            //requestContext.addZuulRequestHeader("JwtAuthorization",authorization);

            if(requestURI.startsWith("/qa")){
                requestContext.addZuulRequestHeader("JwtAuthorization",authorization);
            }
        }
        return null;
    }
}
